﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Ycm20250401.Service._20250410
{
    public class ReturnFacade
    {
        private readonly OrderService20250410 _orderService;
        private readonly InventoryService _inventoryService;
        private readonly PaymentService _paymentService;
        private readonly NotificationService _notificationService;

        // 使用构造函数注入依赖
        public ReturnFacade(
            OrderService20250410 orderService,
            InventoryService inventoryService,
            PaymentService paymentService,
            NotificationService notificationService)
        {
            _orderService = orderService;
            _inventoryService = inventoryService;
            _paymentService = paymentService;
            _notificationService = notificationService;
        }

        public void ProcessReturn(string orderId)
        {
            // 1. 获取订单信息
            var order = _orderService.GetOrder(orderId);

            // 2. 验证退货条件
            if (!_orderService.ValidateReturn(order))
            {
                throw new InvalidOperationException("订单当前状态不允许退货");
            }

            // 3. 恢复库存
            _inventoryService.RestockItems(order.ProductIds);

            // 4. 执行退款
            if (!_paymentService.RefundPayment(order))
            {
                throw new InvalidOperationException("退款失败");
            }

            // 5. 发送通知
            _notificationService.SendReturnConfirmation(order.UserEmail, order.OrderId);

            Console.WriteLine("退货流程已完成");
        }
    }

}
